<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="#">全部</a>
                </li>
                <li
                  v-for="(subjectOne, index) in subjectOneList"
                  :key="index"
                  :class="{ active: oneIndex == index }"
                >
                  <a
                    :title="subjectOne.label"
                    href="#"
                    @click="searchOne(subjectOne.id, index)"
                    >{{ subjectOne.label }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li
                  v-for="(subjectTwo, index) in subjectTwoList"
                  :key="index"
                  :class="{ active: twoIndex == index }"
                >
                  <a
                    :title="subjectTwo.label"
                    href="#"
                    @click="searchTwo(subjectTwo.id, index)"
                    >{{ subjectTwo.label }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange':pageQuery.search.viewcount!=''}">
                <a title="关注度" href="#" @click="searchViewCount()"
                  >关注度&nbsp;<span
                    :class="{ hide: pageQuery.search.viewcount == '' }"
                    >↓</span
                  ></a
                >
              </li>
              <li :class="{'current bg-orange':pageQuery.search.gmtcreate!=''}">
                <a title="最新" href="#" @click="searchCreate()"
                  >最新&nbsp;<span
                    :class="{ hide: pageQuery.search.gmtcreate == '' }"
                    >↓</span
                  ></a
                >
              </li>
              <li :class="{'current bg-orange':pageQuery.search.price!=''}">
                <a title="价格" href="#" @click="searchPrice()"
                  >价格&nbsp;
                  <span :class="{ hide: pageQuery.search.price == '' }">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="pageQuery.total == 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >没有相关数据，小编正在努力整理中...</span
            >
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list" v-if="pageQuery.total > 0">
            <ul class="of" id="bna">
              <li v-for="course in courseList" :key="course.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :src="course.cover"
                      class="img-responsive"
                      :alt="course.title"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/' + course.id"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/' + course.id"
                      :title="course.title"
                      class="course-title fsize18 c-333"
                      >{{ course.title }}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green">
                      <i class="c-fff fsize12 f-fA">{{
                        Number(course.price) == 0 ? "免费" : course.price
                      }}</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ course.viewCount }}</i>
                      |
                      <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <!-- 分页 -->
        <el-pagination
          :current-page="pageQuery.current"
          :page-size="pageQuery.size"
          :total="pageQuery.total"
          style="padding: 30px 0; text-align: center"
          layout="total, prev, pager, next, jumper"
          @current-change="getList"
        />
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import courseApi from "@/api/course";
export default {
  data() {
    return {
      pageQuery: {
        current: 1, //当前页
        search: {
          viewcount:"",
          gmtcreate:"",
          price:""
        },
        size: 4, //每页记录数
        total: 0, //总记录数
      }, //条件封装对象
      courseList: [],
      subjectOneList: [],
      sunjectTwoList: [],
      oneIndex: -1,
      twoIndex: -1,
    };
  },
  created() {
    this.getList();
    this.getAllSubject();
  },
  methods: {
    //查询分页
    getList(current = 1) {
      (this.pageQuery.current = current),
        courseApi.getCoursePage(this.pageQuery).then((response) => {
          this.courseList = response.data.records;
          this.pageQuery.total = response.data.total;
        });
    },

    //查询课程分类
    getAllSubject() {
      courseApi.getSubjectList().then((response) => {
        this.subjectOneList = response.data;
         console.log(this.subjectOneList)
      });
    },

    //处理点击事件
    searchOne(subjectOneId, index) {
      this.oneIndex = index;
      this.twoIndex = -1;
      this.subjectTwoList = [];
      for (let i = 0; index < this.subjectOneList.length-1; i++) {
        var subjectOne = this.subjectOneList[i];
        console.log(subjectOne)
        if (subjectOne.id == subjectOneId) {
          this.subjectTwoList = subjectOne.children;
          console.log("+++++++++++++++++++")
          console.log(this.subjectTwoList)
          break
        }
      }
      this.pageQuery.search.subjectparentid = subjectOneId;
      this.pageQuery.search.subjectid = "";
      this.getList();
    },

    searchTwo(subjectTwoId, index) {
      //把index赋值,为了样式生效
      this.twoIndex = index;
      //把二级分类点击id值，赋值给searchObj
      this.pageQuery.search.subjectid = subjectTwoId;
      //点击某个二级分类进行条件查询
      this.getList();
    },

    searchViewCount(){

      this.pageQuery.search.viewcount="1",
      this.pageQuery.search.gmtcreate="",
      this.pageQuery.search.price="",

      this.getList();

    },

    searchCreate(){

      this.pageQuery.search.viewcount="",
      this.pageQuery.search.gmtcreate="1",
      this.pageQuery.search.price="",

      this.getList();


    },

    searchPrice(){
      this.pageQuery.search.viewcount="",
      this.pageQuery.search.gmtcreate="",
      this.pageQuery.search.price="1",

      this.getList();
    }
  },
};
</script>
<style scoped>
.active {
  background: #bdbdbd;
}
.hide {
  display: none;
}
.show {
  display: block;
}
</style>